﻿// Puts the Chat Toolbar in the proper position.
function relocateChatBar() {
    var elem = document.getElementById('chatBar');
    var height = getViewPortHeight();
    var width = getViewPortWidth();
    var top = height - 30;
    var left = width - (width - 115);
    elem.style.width = width - 140 + "px";
    elem.style.top = top + "px";
    elem.style.left = left + "px";
}

// Puts the Chat Window popup in the proper position.
function relocateChatWindowPopup(itemIndex) {
    var elem = document.getElementById('divChatWindowPopup');
    var height = getViewPortHeight();
    var width = getViewPortWidth();
    var top = height - 335;
    var left = width - (width - 260) + (itemIndex * 155);
    //elem.style.width = width - 300 + "px";
    elem.style.top = top + "px";
    elem.style.left = left + "px";
}

// Puts the Online Users popup in the proper position.
var shown = false;
function relocateOnlineUsersDiv() {
    if (shown == false)
        return;
    var elem = document.getElementById('Chat_OnlineUsersDiv');
    var height = getViewPortHeight();
    var width = getViewPortWidth();
    var top = height - 130;
    var left = width - 210;
    elem.style.top = top + "px";
    elem.style.left = left + "px";
}

// Shows/Hides the Online Users popup.
function showOnlineUsers() {
    var elem = document.getElementById('Chat_OnlineUsersDiv');
    var button = document.getElementById('chatbutton');
    if (shown) {
        elem.style.top = "-4000px";
        elem.style.left = "-4000px";
        button.value = "Show Online Users";
    }
    else {
        var height = getViewPortHeight();
        var width = getViewPortWidth();
        var top = height - 115;
        var left = width - 210;
        elem.style.top = top + "px";
        elem.style.left = left + "px";
        button.value = "Hide Online Users";
    }
    shown = !shown;
}

// Shows/Hides the Chat Window popup.
lastItemIndex = -1;
function showChatWindow(roomID, roomName, itemIndex) {
    document.getElementById('chatarea').value = '';
    document.getElementById('chatWindowTitle').innerHTML = roomName;
    document.getElementById('chatWindowRoomID').value = roomID;
    if (lastItemIndex == itemIndex) {
        document.getElementById('divChatWindowPopup').style.display = 'none'
        lastItemIndex = -1;
    }
    else {
        document.getElementById('divChatWindowPopup').style.display = '';
        lastItemIndex = itemIndex;
    }
    relocateChatWindowPopup(itemIndex);
    loadChatMessagesBox();
}

// Hides the Chat Window popup when the user clicks the minimize button
function hideRoom() {
    document.getElementById('divChatWindowPopup').style.display = 'none';
    lastItemIndex = -1;
}

// Loads the Chat Toolbar.
function loadChatBar() {
    url = "AjaUtils/Chatbar.aspx";
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    var elem = document.getElementById('chatBar');
    
    elem.innerHTML = xmlhttp.responseText;
    setTimeout("loadChatBar()", 2000);   
}

// Loads the Online Users popup.
function loadOnlineUsers() {
    url = 'AjaUtils/OnlineUsersGrid.aspx';
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    var elem = document.getElementById('Chat_OnlineUsersDiv');
    
    elem.innerHTML = xmlhttp.responseText;
}

// Loads the Chat Window popup.
function loadChatWindow(url) {
    //url = "AjaUtils/ChatWindow.aspx";
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    var elem = document.getElementById('divChatWindowPopup');

    elem.innerHTML = xmlhttp.responseText;    
}

// Loads the Chat Messages textbox within the Chat Window popup.
function loadChatMessagesBox() {
    url = "AjaUtils/ChatMessagesTxtbox.aspx";
    url = url + "?roomID=" + URLEncode(document.getElementById("chatWindowRoomID").value);
    //alert(url);

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    var elem = document.getElementById('chatWindowMessagesArea');
    elem.innerHTML = xmlhttp.responseText;
    document.getElementById('messagearea').scrollTop = document.getElementById('messagearea').scrollHeight;
    intervalID = setTimeout("loadChatMessagesBox()", 1234);
}

// Sends a chat message
function sendMessage() {
    var msg = document.getElementById("chatarea").value;
    var roomID = document.getElementById("chatWindowRoomID").value;
    var senderID = document.getElementById("currentUserID").value;
    //var targetID = document.getElementById("targerUserID").value;

    url = "AjaUtils/ChatWindowBox.aspx";
    url = url + "?action=sendmsg";
    url = url + "&msg=" + URLEncode(msg);
    url = url + "&roomID=" + URLEncode(roomID);
    url = url + "&senderID=" + URLEncode(senderID);
    //url = url + "&targetID=" + URLEncode(targetID);
    //alert(url);
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();        
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);

    response = xmlhttp.responseText;
    //alert(response);
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));

    if (response.indexOf("Success") > -1) {
        var msg = document.getElementById("chatarea").value = '';
        loadChatMessagesBox();
    }
}

// Simulates a "click" on send button
function chatAreaOnEnter(evt, frm) {
    var keyCode = null;
    
    if (evt.which) {
        keyCode = evt.which;
    } else if (evt.keyCode) {
        keyCode = evt.keyCode;
    }
    if (13 == keyCode) {
        alert('pressed enter!');
        sendMessage();
    }
    
}

// Highlights the row clicked by the user in the Online Users popup.
var lastClickedUser = null;
var lastClickedUserBg = null;
function selectOnlineUsersTableElement(elemID) {
    if (lastClickedUser != null) {
        document.getElementById(lastClickedUser).style.backgroundColor = lastClickedUserBg;
    }
    var elem = document.getElementById(elemID);
    lastClickedUser = elemID;
    lastClickedUserBg = elem.style.backgroundColor;
    elem.style.backgroundColor = "#FEF0C9";
}

// Creates a new chatroom when the user double-clicks a row in the Online Users popup.
function createChatRoom(targetUser, roomTitle) {
    var url = "AjaUtils/Chatbar.aspx";
    url = url + "?action=createroom";
    url = url + "&creatorID=" + URLEncode(txtCurrentUser.value);
    url = url + "&targetID=" + URLEncode(targetUser);
    url = url + "&roomName=" + URLEncode(roomTitle);
    var response = SubmitReminderForm(url);
    if (response.indexOf("Success") > -1)
        loadChatBar();
}

function leaveRoom() {
    var roomID = document.getElementById("chatWindowRoomID").value;
    var userID = document.getElementById("currentUserID").value;
    //var targetID = document.getElementById("targerUserID").value;

    url = "AjaUtils/ChatWindowBox.aspx";
    url = url + "?action=leaveroom";
    url = url + "&userID=" + URLEncode(userID);
    url = url + "&roomID=" + URLEncode(roomID);

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);

    response = xmlhttp.responseText;
    alert(response);
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));

    if (response.indexOf("Success") > -1) {
        hideRoom();
    }
}

